//
//  IdentityInfomationVC.m
//  Peso Guide
//
//  Created by macbook on 2025/6/12.
//

#import "IdentityInfomationVC.h"
#import "IdentityInfomationCell.h"
#import "IDCardFaceIDVC.h"
#import "IDCardExampleVC.h"

@interface IdentityInfomationVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *bgScrollView;

@property (nonatomic, strong) UIView *idTypeView;
@property (nonatomic, strong) UIView *otherTypeView;
@property (nonatomic, strong) UILabel *idTypeLabel;
@property (nonatomic, strong) UIImageView *idTypeImg;
@property (nonatomic, strong) BaseTableView *tableView1;
@property (nonatomic, strong) BaseTableView *tableView2;
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, assign) BOOL isOtherShow;
@property (nonatomic, strong) UIImageView *rightImg;
@property (nonatomic, strong) UIImageView *otherImg;
@property (nonatomic, strong) UIImageView *otherImgRightImg;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) NSMutableArray *idtypesArr;
@property (nonatomic, strong) NSMutableArray *othertypesArr;
@property (nonatomic, strong) NSDictionary *normally;
@property (nonatomic, strong) NSDictionary *prefix;

@end

@implementation IdentityInfomationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navView.backgroundColor = ColorHex(@"#1A7A35");
    self.titleLabel.text = [Lang set:@"Identity information"];
    self.backColor = ColorHex(@"#F3F4F8");
    
    CAGradientLayer *layer = [CAGradientLayer new];
    layer.colors = @[(__bridge id)ColorHex(@"#FFBD31").CGColor, (__bridge id)ColorHex(@"#FFFFFF").CGColor];
    layer.startPoint = CGPointMake(0.0, 0.0);
    layer.endPoint = CGPointMake(0.0, 1);
    layer.frame = CGRectMake(0, 0, KScreenWidth, kStarBarHeight+188);
    [self.view.layer addSublayer:layer];
    
    UILabel *topLabel = [PesoKit createLabelWithFrame:CGRectMake(0, kStarBarHeight, KScreenWidth, 47) backGroundColor:[UIColor clearColor] text:[Lang set:@"Select an i d to verify your identity"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 14)];
    topLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:topLabel];
    
    _isShow = YES;
    _isOtherShow = NO;

    _bgScrollView = [PesoKit createScrollViewWithFrame:CGRectMake(15, kStarBarHeight+47, KScreenWidth-30, KScreenHeight-kStarBarHeight-47-kSafeBottomHeight) backGroundColor:[UIColor clearColor]];
    _bgScrollView.showsVerticalScrollIndicator = NO;
    _bgScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_bgScrollView];
    
    _idTypeView = [PesoKit createViewWithFrame:CGRectMake(0, 0, KScreenWidth-30, 42+47*5) backGroundColor:ColorHex(@"#FFFFFF")];
    _idTypeView.clipsToBounds = YES;
    _idTypeView.layer.cornerRadius = 21.0f;
    _idTypeView.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:_idTypeView];
    
    _otherTypeView = [PesoKit createViewWithFrame:CGRectMake(0, kStarBarHeight+47+42+47*5+24, KScreenWidth-30, 42) backGroundColor:ColorHex(@"#FFFFFF")];
    _otherTypeView.clipsToBounds = YES;
    _otherTypeView.layer.cornerRadius = 21.0f;
    [_bgScrollView addSubview:_otherTypeView];
    
    _otherImg = [PesoKit createImageViewWithFrame:CGRectMake(0, 0, KScreenWidth, 42) imageNamed:@"ic_identity_2"];
    _otherImg.userInteractionEnabled = YES;
    [_otherTypeView addSubview:_otherImg];
    
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(otherAgreementButtonClick)];
    [_otherImg addGestureRecognizer:tapGesture2];
    
    _otherImgRightImg = [PesoKit createImageViewWithFrame:CGRectMake(KScreenWidth-23*2-7, 15, 7, 12) imageNamed:@""];
    _otherImgRightImg.image = [UIImage imageNamed:@"ic_arrow_right_white"];
    [_otherTypeView addSubview:_otherImgRightImg];
    
    _idTypeImg = [PesoKit createImageViewWithFrame:CGRectMake(0, 0, KScreenWidth, 42) imageNamed:@"ic_identity_2"];
    _idTypeImg.userInteractionEnabled = YES;
    [_idTypeView addSubview:_idTypeImg];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementButtonClick)];
    [_idTypeImg addGestureRecognizer:tapGesture];
    
    _rightImg = [PesoKit createImageViewWithFrame:CGRectMake(KScreenWidth-23*2-7, 15, 7, 12) imageNamed:@""];
    _rightImg.image = [UIImage imageNamed:@"ic_arrow_right_white"];
    [_idTypeView addSubview:_rightImg];
    
    _idTypeLabel = [PesoKit createLabelWithFrame:CGRectMake(13, 0, _idTypeView.width, 42) backGroundColor:[UIColor clearColor] text:[Lang set:@"Recommended ID Type"] textColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleMedium, 16)];
    [_idTypeView addSubview:_idTypeLabel];
    
    UILabel *otherLabel = [PesoKit createLabelWithFrame:CGRectMake(13, 0, _idTypeView.width, 42) backGroundColor:[UIColor clearColor] text:[Lang set:@"Other Options"] textColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleMedium, 16)];
    [_otherTypeView addSubview:otherLabel];
    
    
    [[AWHttpService service] postWithUrl:@"/trains/imports" params:@{@"across":self.productId} succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            self.dataDic = [NSDictionary safeDictionaryWithDictionary:[responseObject safeObjectForKey:@"associating"]];
            self.normally = [NSDictionary safeDictionaryWithDictionary:[self.dataDic safeObjectForKey:@"normally"]];
            self.prefix = [NSDictionary safeDictionaryWithDictionary:[self.dataDic safeObjectForKey:@"prefix"]];

            self.idtypesArr = [NSMutableArray safeArrayWithArray:[self.dataDic safeObjectForKey:@"organizing"]];
            self.othertypesArr = [NSMutableArray safeArrayWithArray:[self.dataDic safeObjectForKey:@"need"]];
            
            self.tableView1.hidden = NO;
            self.tableView2.hidden = YES;
            self.tableView1.height = 47*self.idtypesArr.count;
            self.tableView2.height = 47*self.othertypesArr.count;
            self.idTypeView.height = 47*self.idtypesArr.count+42;
            self.otherTypeView.height = 42;
            self.otherTypeView.top = self.idTypeView.bottom+24;
            self.bgScrollView.contentSize = CGSizeMake(KScreenWidth-30, 47*self.idtypesArr.count+42+42+47*self.othertypesArr.count+100);
            [self.tableView1 reloadData];
            [self.tableView2 reloadData];
        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        
    }];

    [self.idTypeView addSubview:self.tableView1];
    [self.otherTypeView addSubview:self.tableView2];
    [self.view addSubview:self.navView];
}

- (void)agreementButtonClick {
    if (_isShow == NO) {
        [UIView animateWithDuration:0.3 animations:^{
            self.isShow = YES;
            self.tableView1.hidden = NO;
            self.idTypeView.height = 42+47*self.idtypesArr.count;
            self.otherTypeView.top = self.idTypeView.bottom+24;
            self.idTypeView.backgroundColor = [UIColor whiteColor];
            self.rightImg.image = [UIImage imageNamed:@"ic_arrow_right_white"];

        } completion:^(BOOL finished) {
            self.idTypeImg.image = [UIImage imageNamed:@"ic_identity_2"];
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            self.isShow = NO;
            self.tableView1.hidden = YES;
            self.idTypeView.height = 42;
            self.otherTypeView.top = self.idTypeView.bottom+24;
            self.idTypeView.backgroundColor = [UIColor clearColor];
            self.rightImg.image = [UIImage imageNamed:@"ic_arrow_right_white"];

        } completion:^(BOOL finished) {
            self.idTypeImg.image = [UIImage imageNamed:@"ic_identity_1"];
        }];
    }
}

- (void)otherAgreementButtonClick {
    if (_isOtherShow == NO) {
        [UIView animateWithDuration:0.3 animations:^{
            self.isOtherShow = YES;
            self.tableView2.hidden = NO;
            self.otherTypeView.height = 42+47*self.othertypesArr.count;
            self.otherTypeView.top = self.idTypeView.bottom+24;
            self.idTypeView.backgroundColor = [UIColor whiteColor];
            self.otherImgRightImg.image = [UIImage imageNamed:@"ic_arrow_right_white"];

        } completion:^(BOOL finished) {
            self.otherImg.image = [UIImage imageNamed:@"ic_identity_2"];
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            self.isOtherShow = NO;
            self.tableView2.hidden = YES;
            self.otherTypeView.height = 42;
            self.otherTypeView.top = self.idTypeView.bottom+24;
            self.idTypeView.backgroundColor = [UIColor clearColor];
            self.otherImgRightImg.image = [UIImage imageNamed:@"ic_arrow_right_white"];

        } completion:^(BOOL finished) {
            self.otherImg.image = [UIImage imageNamed:@"ic_identity_1"];
        }];
    }
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.idtypesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *IdentityInfomationCellIdentifier = [NSString stringWithFormat:@"IdentityInfomationCellIdentifier%ld",(long)indexPath.row];
    IdentityInfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:IdentityInfomationCellIdentifier];
    if (!cell) {
        cell = [[IdentityInfomationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IdentityInfomationCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (tableView.tag == 456721) {
        if (self.idtypesArr.count > 0) {
            cell.nameString = [self.idtypesArr safeObjectAtIndex:indexPath.row];
        }
    }else {
        if (self.othertypesArr.count > 0) {
            cell.nameString = [self.othertypesArr safeObjectAtIndex:indexPath.row];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 47;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *chooseType = @"";
    
    if (tableView.tag == 456721) {
        chooseType = [NSString stringWithFormat:@"%@",[self.idtypesArr safeObjectAtIndex:indexPath.row]];
    }else {
        chooseType = [NSString stringWithFormat:@"%@",[self.othertypesArr safeObjectAtIndex:indexPath.row]];
    }
        
    NSString *startTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"upLogType-2-startTime"];
    NSString *lng = [[NSUserDefaults standardUserDefaults] objectForKey:@"upLogType-2-lng"];
    NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"upLogType-2-lat"];
    NSString *endTime = [DataPointManager getCurrentTimestamp];
    
    [[DataPointManager sharedManager] senRequestWithType:@"2" startTime:startTime endTime:endTime lng:lng lat:lat orderId:@""];

//    [[DataPointManager sharedManager] logEventWithType:@"2" startTime:startTime endTime:endTime orderId:@""];
    
    IDCardExampleVC *vc = [[IDCardExampleVC alloc] init];
    vc.chooseType = chooseType;
    vc.idCardFaceIDVC = self.idCardFaceIDVC;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - lazy load
- (BaseTableView *)tableView1 {
    if (!_tableView1) {
        _tableView1 = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 42, KScreenWidth-30, 0.01) style:UITableViewStylePlain];
        _tableView1.backgroundColor = ColorHex(@"#FFFFFF");
        _tableView1.delegate = self;
        _tableView1.dataSource = self;
        _tableView1.scrollEnabled = NO;
        _tableView1.tag = 456721;
    }
    return _tableView1;
}

- (BaseTableView *)tableView2 {
    if (!_tableView2) {
        _tableView2 = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 42, KScreenWidth-30, 0.01) style:UITableViewStylePlain];
        _tableView2.backgroundColor = ColorHex(@"#FFFFFF");
        _tableView2.delegate = self;
        _tableView2.dataSource = self;
        _tableView2.scrollEnabled = NO;
        _tableView2.tag = 456722;
    }
    return _tableView2;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
